package com.itcms.webservice.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.itcms.common.util.DateUtil;
import com.itcms.common.util.ReadConfig;
import com.itcms.webservice.uuv.org.ADDepartmentInfo;
import com.itcms.webservice.uuv.org.ArrayOfADDepartmentInfo;
import com.itcms.webservice.uuv.org.UUVOrgServicesSoap;
import com.itcms.webservice.uuv.todoCount.SendTodoCountResponse;
import com.itcms.webservice.uuv.todoCount.SendTodoCountResponse.SendTodoCountResult;
import com.itcms.webservice.uuv.todoCount.TodoCountServiceSoap;

public class WSClientUtil {
	public static void main(String[] args) {
		testOrg();
	}
	
	public static  void testTodoCount() {
		Object o = uuvSoapFactoryBean("http://172.16.84.20:32069/BizTalkESB/10005.asmx",TodoCountServiceSoap.class);
		TodoCountServiceSoap todoCountServiceSoap = (TodoCountServiceSoap) o;
		try {
			String appID = "a8eae716-8607-471a-b34e-bc226a6b4e7c";
			String userCode = "UVU00000002";
			String loginID = "testgdwa";
			SendTodoCountResult countResult = todoCountServiceSoap.sendTodoCount(appID, userCode, loginID, 4);
			System.out.println(countResult.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Integer testOrg() {
//		Object o = uuvSoapFactoryBean("http://172.16.84.20:32069/BizTalkESB/10002.asmx",UUVOrgServicesSoap.class);
		Object o = uuvSoapFactoryBean(ReadConfig.getString("uuv_org"),UUVOrgServicesSoap.class);
		UUVOrgServicesSoap uuvOrgServicesSoap = (UUVOrgServicesSoap) o;
		try {
			XMLGregorianCalendar beginLastUpdateDate = dateToXmlDate(DateUtil
					.getDate("2010-01-01", DateUtil.DATE_FORMAT));
			/*XMLGregorianCalendar endLastUpdateDate = dateToXmlDate(DateUtil
					.getDate("2017-04-28", DateUtil.DATE_FORMAT));*/
			XMLGregorianCalendar endLastUpdateDate = WSClientUtil.dateToXmlDate(DateUtil.getNextDate());
			System.out.println(endLastUpdateDate);
			ArrayOfADDepartmentInfo arrayOfADDepartmentInfo = uuvOrgServicesSoap
					.getUUVLocalOrgList(beginLastUpdateDate, endLastUpdateDate);
			List<ADDepartmentInfo> departmentInfos = arrayOfADDepartmentInfo
					.getADDepartmentInfo();
			System.out.println(departmentInfos.size());
			return departmentInfos.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Object uuvSoapFactoryBean(String url, Class<?> clazz) {
		// TODO Auto-generated method stub
		JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
		soapFactoryBean.setAddress(url);
		soapFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
		soapFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
		// 添加拦截器
		soapFactoryBean.getOutInterceptors().add(new AddSoapHeader());
		soapFactoryBean.setServiceClass(clazz);
		Object o = soapFactoryBean.create();
		return o;
	}

	/**
	 * 将Date类转换为XMLGregorianCalendar
	 * 
	 * @param date
	 * @return
	 */
	public static XMLGregorianCalendar dateToXmlDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		DatatypeFactory dtf = null;
		try {
			dtf = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
		}
		XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar();
		dateType.setYear(cal.get(Calendar.YEAR));
		// 由于Calendar.MONTH取值范围为0~11,需要加1
		dateType.setMonth(cal.get(Calendar.MONTH) + 1);
		dateType.setDay(cal.get(Calendar.DAY_OF_MONTH));
		dateType.setHour(cal.get(Calendar.HOUR_OF_DAY));
		dateType.setMinute(cal.get(Calendar.MINUTE));
		dateType.setSecond(cal.get(Calendar.SECOND));
		dateType.setMillisecond(cal.get(Calendar.MILLISECOND));
		return dateType;
	}
	
	/** 
     * 将XMLGregorianCalendar转换为Date 
     * @param cal 
     * @return  
     */  
    public static Date xmlDate2Date(XMLGregorianCalendar cal){ 
        return cal.toGregorianCalendar().getTime();  
    }  
    
    /** 
     * 规范日期格式 
     * @param cal 
     * @return  
     */  
    public static Date formatDate(Date date){ 
    	String dateString = DateUtil.getDateStr(date, DateUtil.TIME_FORMAT);
		return DateUtil.getDate(dateString, DateUtil.TIME_FORMAT);
    }  
}
